package com.msb.eurekaserver;


import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.http.client.ResponseHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class MainController {

    @Autowired
    DiscoveryClient client;

    @Autowired
    EurekaClient client2;

    @Autowired
    LoadBalancerClient LBClient3;

    @GetMapping("/getHi")
    public String getHi(){
        return "Hi";
    }

    @GetMapping("/client")
    public String client(){
        List<String> services = client.getServices();
        for (String service : services) {
            System.out.println(service);
            /*
            eureka-server
            provider
            consumer
            */
        }
        return "client";
    }

    @GetMapping("/client2")
    public Object client2(){
        return client.getInstances("provider");
        /*for (ServiceInstance service : provider) {
            System.out.println(service);
        }*/
        // System.out.println(ToStringBuilder.reflectionToString(provider));
    }

    @GetMapping("/client3")
    public Object client3(){
        List<ServiceInstance> instances = client.getInstances("provider");
        for (ServiceInstance ins : instances) {
            System.out.println(ToStringBuilder.reflectionToString(ins));
            // 输出： org.springframework.cloud.netflix.eureka.EurekaServiceInstance@7f3d3f9d[instance=InstanceInfo [instanceId = localhost:provider:8001, appName = PROVIDER, hostName = localhost, status = UP, ipAddr = 192.168.159.1, port = 8001, securePort = 443, dataCenterInfo = com.netflix.appinfo.MyDataCenterInfo@54ae74b]
        }
        return "client3";
    }

    @GetMapping("/client4")
    public Object client4(){
        // 使用服务名，找列表
        List<InstanceInfo> instances = client2.getInstancesByVipAddress("provider", false);
        // 具体服务
        // instances = client2.getInstancesById("localhost:provider:8001");
        for (InstanceInfo ins : instances) {
            System.out.println(ToStringBuilder.reflectionToString(ins));
        }
        if(instances.size()>0){
            InstanceInfo instanceInfo = instances.get(0);
            if(instanceInfo.getStatus() == InstanceInfo.InstanceStatus.UP ){
                String url = "http://"+instanceInfo.getHostName()+":"+instanceInfo.getPort()+"/getHi";
                System.out.println("url: "+url);
                RestTemplate restTemplate = new RestTemplate();
                String resStr = restTemplate.getForObject(url, String.class);
                System.out.println("resStr: "+resStr);
                return resStr;
            }
        }
        return "client4";
    }

    @GetMapping("/client5")
    public Object client5(){

        //Ribbon完成客户端的负载均衡，过滤down下线了的节点
        ServiceInstance instance = LBClient3.choose("provider");
        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/getHi";
        System.out.println("url: "+url);
        RestTemplate restTemplate = new RestTemplate();
        String resStr = restTemplate.getForObject(url, String.class);
        System.out.println("resStr: "+resStr);
        return resStr;
    }
}
